
var type = 0;
var imgPath = '';
$(function () {

    var id;
    var order;//标记用户点击的信息索引值，为信息的修改删除做定位标记
    //1，点击删除按钮删除相应信息词条
    $('#f-table').on('click','.bg-color2',function(){
        // id = $(this)('#f-table').val();
        id =$(this).parents('tr').attr('class');
    })
    $('#deleteClient').click(function () {
        $.ajax({
            type: "POST",
            url: "../user/deleteClient",
            dataType:'json',
            data:{order:id},
            success: function(data)
            {
                if (data==0){
                    // alert("删除成功！")
                }else if (data==1){
                    alert("删除失败！")
                }else {
                    alert("系统异常！")
                }
                window.location.href = "../user/getClient";
            }
        });
    });


    //删除用户
    var id2;
    var order2;//标记用户点击的信息索引值，为信息的修改删除做定位标记
    //1，点击删除按钮删除相应信息词条
    $('#f-table').on('click','.bg-color2',function(){
        id2 =$(this).parents('tr').attr('class');
    })
    $('#deleteUser').click(function () {
        $.ajax({
            type: "POST",
            url: "../user/deleteUser",
            dataType:'json',
            data:{order2:id2},
            success: function(data)
            {
                if (data==0){
                    // alert("删除成功！")
                }else if (data==1){
                    alert("删除失败！")
                }else {
                    alert("系统异常！")
                }
                window.location.href = "../user/getUser";
            }
        });
    });


    //添加客戶信息
    $('#addClientUser').click(function () {
        var name = $('.name-input').val();
        var legalPerson = $('.owner-input').val();
        var photo = "";
        if(imgPath == ""||imgPath == null)
        {
            photo = $('#ImgPr').attr('');
        }else{
            photo = imgPath;
        }
        var address = $('.coords-input').val();
        var tel = $('.call-input').val();
        var linkMan = $('.person-input').val();
        var mobile = $('.tel-input').val();
        var email = $('.email-input').val();
        var phone = $('.zuoji-input').val();
        var serverManager = $('.client-input').val();



        var json = {
            "name":name,
            "legalPerson":legalPerson,
            "licenseUrl":photo,
            "address":address,
            "tel":tel,
            "linkMan":linkMan,
            "mobile":mobile,
            "email":email,
            "phone":phone,
            "serverManager":serverManager
        };

        var userjson = JSON.stringify(json);
        // alert(userjson);
        $.ajax({
            type: "POST",
            url: "../user/addClient",
            dataType:'json',
            data:{
                "userJson":userjson
            },
            success:function(data){
                if (data==1){
                    // alert("添加成功")
                } else if(data==0){
                    alert("添加失败")
                }
                window.location.href = "../user/getClient";
            }
        });
    });


    //删除客户信息
    $('#deleteClient').click(function () {

        var id = $();
        // alert(userjson);
        $.ajax({
            type: "POST",
            url: "../user/addClient",
            dataType:'json',
            data:{
                "userJson":userjson
            },
            success:function(data){
                if (data==1){
                    // alert("删除成功")
                } else if(data==0){
                    alert("删除失败")
                }else {
                    alert("系统异常")
                }
                window.location.href = "../user/getClient";
            }
        });
    });


    $('#updateClientUser').click(function () {
        saveOrUpdateClientUser();

    });

    //添加或修改普通用户
    $('#addCommonUser').click(function () {
        // alert(11111);
        saveOrUpdateUser();
    })

    //添加用户按妞
    $('#add_info').click(function () {
        var action = "";
        var name = $(this).attr('name');
        if(name == "addClient")
        {
            action = "1_0";
            type = 2;
        }
        else if(name == "addUser")
        {
            action = "2_0";
            type = 3;
        }
        //鉴权
        $.ajax({
            type: "POST",
            url: "../user/checkPower",
            dataType:'json',
            data:{
                "action":action
            },
            success:function(data){
                if(data.status != "success")
                {
                    alert(data.msg);
                    $('#pop_info').css('display','none');//弹出新建信息页面
                    $('#pop_info').find('input').val('');//初始化新建信息页面
                    $('#new_add').css('display','none');//显示新建信息页面的按钮组
                    $('#txt_add').css('display','block');//关闭修改信息页面的按钮组
                    // alert(2222);
                    return false;
                }
                else {
                    // alert(1111);
                }
            }
        });

    });


    $('.update_info').click(function () {
        var action = "";
        var name = $(this).attr('name');
        if(name == "addClient")
        {
            action = "1_2";
            type = 2;
        }
        else if(name == "addUser")
        {
            action = "2_2";
            type = 3;
        }

        //鉴权
        $.ajax({
            type: "POST",
            url: "../user/checkPower",
            dataType:'json',
            data:{
                "action":action
            },
            success:function(data){
                if(data.status != "success")
                {
                    alert(data.msg);
                    $('#pop_info').css('display','none');//弹出新建信息页面
                    $('#pop_info').find('input').val('');//初始化新建信息页面
                    $('#new_add').css('display','none');//显示新建信息页面的按钮组
                    $('#txt_add').css('display','block');//关闭修改信息页面的按钮组
                    // alert(2222);
                    return false;
                }
                else {
                    // alert(1111);
                }
            }
        });

    });



    //修改客户

    $('#updateClient').click(function () {
        var id = $('#clientId').val();
        var name = $('.name-input').val();
        var legalPerson = $('.owner-input').val();
        var address = $('.coords-input').val();
        var phone = $('.call-input').val();
        var photo = "";
        if(imgPath == ""||imgPath == null)
        {
            photo = $('#ImgPr').attr('');
        }else{
            photo = imgPath;
        }
        var linkMan = $('.person-input').val();
        var mobile = $('.tel-input').val();
        var email = $('.email-input').val();
        var tel = $('.zuoji-input').val();
        var serverManager = $('.client-input').val();
        var json = {
            "id":id,
            "name":name,
            "legalPerson":legalPerson,
            "licenseUrl":photo,
            "address":address,
            "tel":tel,
            "linkMan":linkMan,
            "mobile":mobile,
            "email":email,
            "phone":phone,
            "serverManager":serverManager
        };

        var userjson = JSON.stringify(json);
        $.ajax({
            type: "POST",
            url: "../user/updateClient",
            dataType:'json',
            data:{
                "userJson":userjson
            },
            success:function(data){
                if (data==1){
                    // alert("修改成功")
                } else if(data==0){
                    alert("修改失败")
                }
                window.location.href = "../user/getClient";
            }
        });

    });


    //修改用户

    $('#updateUser').click(function () {
        var id = $('#userId').val();
        var name = $('.card-input').val();
        var password = $('.code-input').val();
        var clientName = $('.owner-input').val();
        var typeinput = $('.type-input').val();
        var userName = $('.name-input').val();
        var tel = $('.tel-input').val();
        var email = $('.email-input').val();
        var node = $('.node-input').val();
        var stationId = $('#carStation').attr('name');
        var json = {
            "id":id,
            "name":name,
            "password":password,
            "clientName":clientName,
            "type":typeinput,
            "userName":userName,
            "tel":tel,
            "email":email,
            "remake":node,
            "stationId":stationId,
        };

        var userjson = JSON.stringify(json);
        $.ajax({
            type: "POST",
            url: "../user/updateUser",
            dataType:'json',
            data:{
                "userJson":userjson
            },
            success:function(data){
                if (data==0){
                    // alert("修改成功")
                } else if(data==1){
                    alert("修改失败")
                }else if(data==3){
                    alert("不可添加同级用户")
                }
                window.location.href = "../user/getUser";
            }
        });

    });

});


function saveOrUpdateClientUser() {
    var name = $('.name-input').val();
    var legalPerson = $('.owner-input').val();
    var address = $('.coords-input').val();
    var phone = $('.call-input').val();
    var linkMan = $('.person-input').val();
    var mobile = $('.tel-input').val();
    var email = $('.email-input').val();
    var tel = $('.zuoji-input').val();
    var serverManager = $('.client-input').val();

    var json = {
        "name":name,
        "legalPerson":legalPerson,
        "address":address,
        "tel":tel,
        "linkMan":linkMan,
        "mobile":mobile,
        "email":email,
        "phone":phone,
        "serverManager":serverManager
    };

    var userjson = JSON.stringify(json);
    // alert(userjson);
    $.ajax({
        type: "POST",
        url: "../user/addClient",
        dataType:'json',
        data:{
            "userJson":userjson
        },
        success:function(data){
            // alert(11);
        }
    });
}


function saveOrUpdateUser(){
    var name = $('.card-input').val();
    var password = $('.code-input').val();
    var clientName = $('.owner-input').val();
    var typeinput = $('.type-input').val();
    var userName = $('.name-input').val();
    var tel = $('.tel-input').val();
    var email = $('.email-input').val();
    var node = $('.node-input').val();
    var carStation = $('#carStation').attr('name');
    // alert(carStation);
    var json = {
        "name":name,
        "password":password,
        "clientName":clientName,
        "type":typeinput,
        "userName":userName,
        "tel":tel,
        "email":email,
        "remake":node,
        "stationId":carStation
    };

    var userjson = JSON.stringify(json);
    $.ajax({
        type: "POST",
        url: "../user/addUser",
        dataType:'json',
        data:{
            "userJson":userjson
        },
        success:function(data){
            if (data==0){
                // alert("添加成功")
            }else if (data==1){
                alert("添加失败")
            }else if (data==2){
                alert("不能添加普通用户")
            } else if (data==3){
                alert("无权限添加客户管理员")
            }else {
                alert("系统异常")
            }
            window.location.href = "../user/getUser";
        }
    });

}

function doUpload() {
    var formData = new FormData($( "#uploadForm" )[0]);
    $.ajax({
        type: 'POST',
        url: '../file/upload' ,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (returndata) {
            imgPath = returndata.data;
            alert("上传成功");
        },
        error: function (returndata) {
            alert("上传失败");
        }
    });
}